import { app, BrowserWindow, ipcMain, Menu, Tray } from 'electron'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow

const mainWindowURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createMainWindow() {

    mainWindow = new BrowserWindow({
      height: 663,
      useContentSize: true,
      width: 1360
    })

    mainWindow.loadURL(mainWindowURL)

    mainWindow.on('closed', () => {
      mainWindow = null
    })

}

let loginWindow;

const loginWindowURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080/#/login`
  : `file://${__dirname}/login.html`

function createLoginWindow() {

  loginWindow = new BrowserWindow({
    frame: false,
    autoHideMenuBar: true,
    resizable: false,
    width: 700,
    height: 400,
    useContentSize: true
  })

  loginWindow.loadURL(loginWindowURL)

  loginWindow.on('closed', () => {
    loginWindow = null
  })
}

// let tray = null

// function createTray() {
//   tray = new Tray("./static/32.png")
//   const contextMenu = Menu.buildFromTemplate([
//     { label: '进入系统', type: 'normal', click: showMainWindow() },
//     { label: '个人设置', type: 'normal', click: hideMainWindow() },
//     { label: '注销', type: 'normal', click: showloginWindow() },
//     { label: '退出', type: 'normal', click: exitSystem() }
//   ])
//   tray.setToolTip('This is my application.')
//   tray.setContextMenu(contextMenu)
// }

// function hideMainWindow() {
//   mainWindow.hide()
// }

// function showMainWindow() {
//   mainWindow.show()
// }

// function showloginWindow() {
//   mainWindow.close()
//   mainWindow = null
//   tray.destroy()
//   createLoginWindow()
// }

// function exitSystem() {
//   tray.destroy()
//   app.quit()
// }


ipcMain.on('min-loginWindow', (event, arg) => {
  console.log(arg)
  loginWindow.minimize()
})

ipcMain.on('close-loginWindow', (event, arg) => {
  console.log(arg)
  loginWindow.close()
})

ipcMain.on('login-system', (event, arg) => {
  console.log(arg)
  createMainWindow()
  loginWindow.close()
})




app.on('ready', createLoginWindow)

app.on('activate', () => {
  // 在macOS上，当单击dock图标并且没有其他窗口打开时，
  // 通常在应用程序中重新创建一个窗口。
  if (loginWindow === null) {
    createLoginWindow()
  }
})


/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
